package com.appistry.storage.domain;

import org.json.JSONException;

import com.appistry.api.rest.domain.RestResponse;
import com.appistry.storage.exception.BadRequestException;
import com.appistry.storage.exception.ConflictException;
import com.appistry.storage.exception.GoneException;
import com.appistry.storage.exception.MultipleChoicesException;
import com.appistry.storage.exception.NotFoundException;
import com.appistry.storage.exception.StorageManagerRestException;

public class AbstractRestDomainObject {
	public static final String PARSE_JSON_ERR = "Unable to parse JSON!";
	public static final String NOT_IMPLEMENT = "Not yet implemented!";

	/**
	 * Helper method that checks a rest response for error codes.
	 * 
	 * @param response
	 * @throws JSONException
	 * @throws FabricManagerRestException
	 */
	protected void checkForErrors(RestResponse response) throws StorageManagerRestException {
		if (response.getStatusCode() != 200 && response.getStatusCode() != 202) {

			switch (response.getStatusCode()) {
			case 300:
				throw new MultipleChoicesException(response);
			case 400:
				throw new BadRequestException(response);
			case 404:
				throw new NotFoundException(response);
			case 409:
				throw new ConflictException(response);
			case 410:
				throw new GoneException(response);
			default:
				throw new StorageManagerRestException(response);
			}
		}
	}

}
